package com.letu.boot.core.handler;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.letu.annotation.annotation.base.BaseControllerInfo;
import com.letu.annotation.annotation.base.IgnoreResponseBodyAdvice;
import com.letu.annotation.base.LanguageTypeEnum;
import com.letu.core.base.R;
import com.letu.core.context.ContextUtil;
import com.letu.core.dynamic.DynamicAnno;
import com.letu.core.dynamic.DynamicData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;

import java.lang.reflect.Method;
import java.util.Date;
import java.util.List;

import static com.letu.core.base.R.SUCCESS_CODE;

/**
 * 全局响应体包装
 *
 * @author ligang
 * @date 2020/12/24 8:09 下午
 */
@Slf4j
public class AbstractGlobalResponseBodyAdvice implements ResponseBodyAdvice {
    @Override
    public boolean supports(MethodParameter methodParameter, Class aClass) {
        //记录vo标识
//        Method method = methodParameter.getMethod();
//        BaseControllerInfo controllerInfo = method.getAnnotation(BaseControllerInfo.class);
//
//        if (controllerInfo != null) {
//            if(controllerInfo.voGroup() != null){
//                Class[] classes = controllerInfo.voGroup();
//                if (classes.length > 0) {
//                    DynamicData.setGroupClassThreadLocal(classes[0]);
//                }
//            }
//            DynamicData.setBaseControllerInfoThreadLocal(controllerInfo);
//        }
        // 类上如果被 IgnoreResponseBodyAdvice 标识就不拦截
//        if (methodParameter.getDeclaringClass().isAnnotationPresent(IgnoreResponseBodyAdvice.class)) {
//            return false;
//        }
//
//        // 方法上被标注也不拦截
//        if (methodParameter.getMethod().isAnnotationPresent(IgnoreResponseBodyAdvice.class)) {
//            return false;
//        }
        BaseControllerInfo baseControllerInfo = DynamicData.getBaseControllerInfoThreadLocal();
        if(baseControllerInfo==null){
            return false;
        }
        return true;
    }

    @Override
    public Object beforeBodyWrite(Object o, MethodParameter methodParameter, MediaType mediaType, Class aClass, ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse) {
        if (o == null) {
            return null;
        }
        if (o instanceof R) {
            /*********简化VO数据结构功能 end**************/
            R result = (R) o;
//                BeanUtils.copyProperties(obj, result);
            if (SUCCESS_CODE == result.getCode()) {
                //data数据
                Long startTime = System.currentTimeMillis();
                if (result.getData() == null) {
                    return o;
                }
                //判断是否是分页数据  PageData
                if (IPage.class.getName().equals(result.getData().getClass().getName())) {
                    IPage pageData = (IPage) result.getData();
                    List listObject = pageData.getRecords();
//                    if (DynamicData.getBaseControllerInfoThreadLocal().isLanguageValue()) {
//                        //中英文值切换
//                        LanguageDynamicAnno.executeSimplVo(listObject);
//                    }
                    if (DynamicData.getGroupClassThreadLocal() != null) {
                        DynamicAnno.executeSimplVo(listObject);
                    }

                    pageData.setRecords(listObject);
                }else if (Page.class.getName().equals(result.getData().getClass().getName())) {
                    Page pageData = (Page) result.getData();
                    List listObject = pageData.getRecords();
//                    if (DynamicData.getBaseControllerInfoThreadLocal().isLanguageValue()) {
//                        //中英文值切换
//                        LanguageDynamicAnno.executeSimplVo(listObject);
//                    }
                    if (DynamicData.getGroupClassThreadLocal() != null) {
                        DynamicAnno.executeSimplVo(listObject);
                    }
                    pageData.setRecords(listObject);
                } else {
                    Object object = result.getData();
                    //简化VO属性功能
//                    if (DynamicData.getBaseControllerInfoThreadLocal().isLanguageValue()) {
//                        //中英文值切换
//                        LanguageDynamicAnno.executeSimplVo(object);
//                    }
                    if (DynamicData.getGroupClassThreadLocal() != null) {
                        DynamicAnno.executeSimplVo(object);
                        log.info("简化VO功能耗时：" + (System.currentTimeMillis() - startTime));
                    }
                }
                return result;
            }
            /*********简化VO数据结构功能 end**************/
            return o;
        }
        return o;
    }

}
